package com.situ.myplayer.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.situ.myplayer.R;
import com.situ.myplayer.service.MusicService;

public class MusicPlayerActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "MusicPlayerActivity";
    //当前正在播放歌曲的路径
    private String mPath;

    private SeekBar mSeekBar;
    private TextView mTvProgress;
    private TextView mTvTotal;
    private ImageView mIvPrevious;
    private ImageView mIvPlay;
    private ImageView mIvNext;

    private MusicService.MusicControll mMusicControll;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected");
            mMusicControll = (MusicService.MusicControll) service;
            mMusicControll.init(mPath);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_player);
        //初始化页面
        initView();
        //初始化数据
        initData();
        //绑定服务播放音乐
        bindMyService();
    }

    private void bindMyService() {
        Intent intent = new Intent(this, MusicService.class);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
    }

    private void initData() {
        Intent intent = getIntent();
        mPath = intent.getStringExtra("path");
    }

    private void initView() {
        mSeekBar = (SeekBar) findViewById(R.id.seekBar);
        mTvProgress = (TextView) findViewById(R.id.tv_progress);
        mTvTotal = (TextView) findViewById(R.id.tv_total);
        mIvPrevious = (ImageView) findViewById(R.id.iv_previous);
        mIvPlay = (ImageView) findViewById(R.id.iv_play);
        mIvNext = (ImageView) findViewById(R.id.iv_next);
        mIvPrevious.setOnClickListener(this);
        mIvPlay.setOnClickListener(this);
        mIvNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_previous:

                break;
            case R.id.iv_play:
                if (mMusicControll.isPlaying()) {
                    mMusicControll.pause();
                    mIvPlay.setImageDrawable(getResources().getDrawable(R.drawable.playbar_play));
                } else {
                    mMusicControll.play();
                    mIvPlay.setImageDrawable(getResources().getDrawable(R.drawable.playbar_pause));
                }

                break;
            case R.id.iv_next:

                break;
        }
    }
}
